const express = require('express');
const router = express.Router();
const mysql = require('../sql/mysql_select');
const fs = require('fs');
const query = require('../sql/query');
const util = require('../utils/util')

/* GET api listing. */

router.get('/xsqk', async function (req, res, next) {
    const { startTime1, endTime1, ztlxs, startTime2, endTime2 } = req.query;
    const res2018 = await mysql.query(query.xsqk(startTime1, endTime1, 'Report_2018_copy', ztlxs));
    const res2019 = await mysql.query(query.xsqk(startTime2, endTime2, 'Report_2019_copy', ztlxs));
    res.json({
        res2018,
        res2019
    })
});

router.get('/mpxsqk', async function(req, res, next) {
    const { startTime1, endTime1, ztlxs, startTime2, endTime2 } = req.query;
    const t1 = await mysql.query(query.mpxsqk(startTime1, endTime1, 'Report_2018_copy', ztlxs));
    const t2 = await mysql.query(query.mpxsqk(startTime2, endTime2, 'Report_2019_copy', ztlxs));
    const res2018 = util.formatMp(t1);
    const res2019 = util.formatMp(t2);
    res.json({
        res2018,
        res2019
    })
})

router.get('/write/', function (req, res, next) {
    fs.writeFile('res.txt', 'test', { flag: 'a+' }, err => {
        err ? console.log(err) : res.json('ok')
    })
});

module.exports = router;
